﻿namespace SugarTank.Mobile.Managers
{
    using Contracts;

    public class ResourceManager : IResourceManager
    {
        #region IResourceManager Members

        /// <summary>
        /// Gets the resource.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        public string GetStringResource<T>(string key)
        {
            var resources = GetResouceManager<T>();

            string result = resources.GetString(key);

            if (!string.IsNullOrEmpty(result))
            {
                return result;
            }

            return string.Empty;
        }

        /// <summary>
        /// Gets the object resource.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <param name="key">The key.</param>
        public TObject GetObjectResource<T, TObject>(string key)
        {
            var resources = GetResouceManager<T>();

            object result = resources.GetObject(key);

            if (result != null)
            {
                return (TObject)result;
            }

            return default(TObject);
        }

        #endregion

        /// <summary>
        /// Gets the resouce manager.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private static System.Resources.ResourceManager GetResouceManager<T>()
        {
            return new System.Resources.ResourceManager(typeof(T).FullName, typeof(T).Assembly);
        }
    }
}